package com.yijia.work5.question3;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class Question3Servlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        if (request.getParameter("n").equals("")){

            System.out.println("数据为空！");
            request.setAttribute("error", "数据为空！！");
            request.getRequestDispatcher("/work5/3.jsp").forward(request, response);

        }else {
            Question3Bean question3Bean = new Question3Bean();

            int n = Integer.parseInt(request.getParameter("n"));
            int sum = question3Bean.adder(n);
            System.out.println("接收到值："+n);
            System.out.println("结果："+sum);

            request.setAttribute("n", n);
            request.setAttribute("sum", sum);
            request.getRequestDispatcher("/work5/3.jsp").forward(request, response);

        }




    }

}
